import os, sys
import pygame
from pygame.locals import *
from time import sleep
from jasgame.io import ImageLoader
from jasgame.core import BaseJasGame
    
class BiaowGame(BaseJasGame):
    def __init__(self):
        BaseJasGame.__init__(self, 'Biaow', "Press Esc to exit, Click to walk and Press any key to spin & bounce")
        self.write_caption("biaow loves jasmine xx")

        self.biaow = Biaow()
        self.allsprites = pygame.sprite.RenderPlain((self.biaow))
        self.clock = pygame.time.Clock()

        self.redraw()
        
    def handle_events(self):
        for event in pygame.event.get():
            if event.type == QUIT:
                return False
            elif event.type == KEYDOWN and event.key == K_ESCAPE:
                return True
            elif event.type == KEYUP and event.key == K_ESCAPE:
                return False
            elif event.type == MOUSEBUTTONDOWN:
                self.biaow._bounce()
            elif event.type == MOUSEBUTTONUP:
                self.biaow.unbounce()
            elif event.type == KEYDOWN:
                self.biaow.walk()
            elif event.type == KEYUP:
                self.biaow.unwalk()
        self.redraw()
        return True

    # def redraw(self):
        # self.allsprites.update()
        # self.screen.blit(self.background, (0, 0))
        # self.allsprites.draw(self.screen)
        # pygame.display.flip()
    
class Biaow(pygame.sprite.Sprite):
    """moves a Biaow on the screen, following the mouse"""
    def __init__(self):
        pygame.sprite.Sprite.__init__(self) #call Sprite initializer
        il= ImageLoader()
        self.image, self.rect = il.load_png('Mallyon-kit.png') #, -1)
        self.walking = 0
        screen = pygame.display.get_surface()
        self.area = screen.get_rect()
        self.rect.topleft = 10, 10
        self.move = 9
        self.dizzy = 0
        self.bouncy = 0

    def update(self):
        "move the biaow based on the mouse position"
        pos = pygame.mouse.get_pos()
        self.rect.midtop = pos
        if self.walking:
            self.rect.move_ip(5, 10)

    def walk(self): #, target):
        "returns true if the biaow collides with the target"
        if not self.walking:
            self.walking = 1
            hitbox = self.rect.inflate(-50, -50)
            self.walked()
            #return hitbox.colliderect(target.rect)

    def unwalk(self):
        "called to pull the biaow back"
        self.walking = 0

    def unbounce(self):
        "called to pull the biaow back"
        self.bouncy = 0
        #self.update()

    def update(self):
        "walk or spin, depending on the biaows state"
        if self.dizzy:
            self._spin()
        elif self.bouncy:
            self._bounce()
        else:
            self._walk()
            
    def _walk(self):
        "move the biaow across the screen, and turn at the ends"
        newpos = self.rect.move((self.move, 0))
        if self.rect.left < self.area.left or \
        self.rect.right > self.area.right:
            self.move = -self.move
            newpos = self.rect.move((self.move, 0))
            self.image = pygame.transform.flip(self.image, 1, 0)
        self.rect = newpos
    
    def _bounce(self):
        "move the biaow across the screen, and turn at the ends"
        newpos = self.rect.move((0,-self.move))
        if self.rect.top < self.area.top or \
        self.rect.bottom > self.area.bottom:
            self.move = -self.move
            newpos = self.rect.move((0,-self.move))
            self.image = pygame.transform.flip(self.image, 1, 0)
        self.rect = newpos
#        self.redraw()
        
        
    def _spin(self):
        "spin the biaow image"
        center = self.rect.center
        self.dizzy = self.dizzy + 12
        if self.dizzy >= 360:
            self.dizzy = 0
            self.image = self.original
        else:
            rotate = pygame.transform.rotate
            self.image = rotate(self.original, self.dizzy)
            self.image = rotate(self.original, self.dizzy)
        self.rect = self.image.get_rect(center=center)

    def walked(self):
        "this will cause the biaow to start spinning"
        if not self.dizzy:
            self.dizzy = 1
            self.original = self.image
        "this will cause the biaow to start spinning"
        if not self.bouncy:
            self.bouncy = 1
            self.original = self.image

def main():
    bgame = BiaowGame()
    conti= True
    while conti:
        bgame.clock.tick(32)
        conti = bgame.handle_events()

    bgame.bye("bye bye biaow")

#this calls the 'main' function when this script is executed
if __name__ == '__main__': main()
